<template>
  <div id="App">
    <div style="padding: 20px">
      <el-button type="primary" size="mini" @click="add(1)" class="add_cb">添加一级导航</el-button>
    </div>
    <el-table :data="tableData" :header-cell-style="{ background: '#E6EFFF' }" style="width: 100%; margin-bottom: 20px"
      row-key="id" :border="true" default-expand-all :tree-props="{ children: 'children' }">
      <el-table-column prop="menuName" label="菜单名称" sortable width="180">
        <template slot-scope="scope">
          <span style="margin-right: 10px">{{ scope.row.menuName }}</span>
          <i class="el-icon-circle-plus" style="color: #3f75ff; font-size: 15px" @click="add(2, scope.row.menuName)"></i>
        </template>
      </el-table-column>
      <el-table-column prop="router" label="路由" sortable width="180">
      </el-table-column>
      <el-table-column prop="menuType" label="菜单类型"> </el-table-column>
      <el-table-column prop="address" label="图标">
        <i class="el-icon-share"></i>
      </el-table-column>
      <el-table-column prop="superior" label="归属上级" sortable width="180">
      </el-table-column>
      <el-table-column prop="num" label="排序" sortable width="180">
      </el-table-column>
      <el-table-column prop="state" label="显示状态">
        <template slot-scope="scope">
          <el-switch disabled v-model="scope.row.state == 1 ? true : false"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="superior" label="添加时间" sortable width="180">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button @click.native.prevent="add(3, scope.row)" type="text" size="small">
            编辑
          </el-button>
          <el-button @click.native.prevent="deleteRow(scope.row.id)" type="text" size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加权限 -->
    <el-dialog :title="title" :visible.sync="centerDialogVisible" width="30%" center>
      <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
          <el-form label-width="100px" ref="form" :rules="fenzhangRules" :model="form">
            <el-form-item label="菜单名称" prop="menuName">
              <el-input placeholder="请输入菜单名称" v-model="form.menuName">
              </el-input>
            </el-form-item>
            <el-form-item label="菜单类型" prop="menuType">
              <el-select v-model="form.menuType" filterable placeholder="请选择菜单类型" style="width: 100%">
                <el-option label="导航" value="1"> </el-option>
                <el-option label="功能子项" value="2"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="路由地址" prop="router">
              <el-input v-model="form.router" placeholder="请输入路由地址">
              </el-input>
            </el-form-item>
            <el-form-item label="归属上级">
              <el-input disabled v-model="form.superior" placeholder="请输入账归属上级"></el-input>
            </el-form-item>
            <el-form-item label="排序设置" prop="num">
              <el-input-number v-model="form.num" @change="handleChange" :min="1" :max="3" label="描述文字"></el-input-number>
            </el-form-item>
            <el-form-item label="显示状态">
              <el-switch v-model="form.state" active-value="1" inactive-value="2"></el-switch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="fenzhangSaveFn">确定</el-button>
              <el-button type="default" @click="cancelFn(1)">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      title: "添加菜单",
      tableData: [
        {
          id: 1,
          superior: "平台",
          menuName: "权限管理",
          menuType: "导航",
          num: "1",
          state: "1",
          router: "menupage/mentuList",
          children: [
            {
              id: 37,
              superior: "权限管理",
              menuName: "菜单权限",
              menuType: "导航",
              num: "1",
              state: "1",
              router: "menupage/mentuList"
            },
            {
              id: 39,
              superior: "权限管理",
              menuName: "组织结构",
              menuType: "导航",
              num: "1",
              state: "1",
              router: "menupage/mentuList",
              children: [
                {
                  id: 36,
                  superior: "组织结构",
                  menuName: "新增组织",
                  menuType: "功能按钮",
                  num: "1",
                  state: "2",
                  router: "menupage/mentuList"
                },
                {
                  id: 40,
                  superior: "组织结构",
                  menuName: "编辑组织",
                  menuType: "功能按钮",
                  num: "2",
                  state: "2",
                  router: "menupage/mentuList"
                }
              ]
            }
          ]
        }
      ],
      centerDialogVisible: false,
      form: {
        menuName: "",
        menuType: "",
        icon: "",
        router: "",
        superior: "",
        num: "",
        state: "1"
      },
      fenzhangRules: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" }
        ],
        menuType: [
          { required: true, message: "请选择菜单类型", trigger: "blur" }
        ],
        router: [
          { required: true, message: "请输入路由地址", trigger: "blur" }
        ],
        num: [{ required: true, message: "请输入排序", trigger: "blur" }]
      }
    }
  },
  watch: {
    centerDialogVisible(newVal) {
      if (!newVal) {
        this.form = {
          menuName: "",
          menuType: "",
          icon: "",
          router: "",
          superior: "",
          num: "",
          state: "1"
        }
      }
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.icon = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg"
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!")
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!")
      }
      return isJPG && isLt2M
    },
    add(type, val) {
      this.centerDialogVisible = true
      if (type == 1) {
        this.form.superior = "平台"
      } else if (type == 2) {
        this.form.superior = val
      } else {
        console.log(val)
        this.form = val
        this.title = "编辑菜单"
      }
    },
    // 删除菜单
    deleteRow(val) {
      this.$confirm("确定删除所选菜单？", {
        confirmButtonText: "确定删除",
        cancelButtonText: "取消",
        type: "error"
      })
        .then(() => {
          console.log(`已删除id为${val}的菜单`)
        })
        .catch(() => { })
    },
    fenzhangSaveFn() {
      this.$refs.form.validate(val => {
        if (val) {
          console.log("参数   ", this.form)
        }
      })
    },
    cancelFn() {
      this.centerDialogVisible = false
    },
    // 排序设置
    handleChange() { },
    // 上传照片
    uploadFile1() { }
  }
}
</script>
<style scoped>
.imgClass {
  width: 90px;
  height: 90px;
  background: #f1f1f1;
  border-radius: 6px 6px 6px 6px;
}

.add_cb {
  width: 110px;
  height: 32px;
  background: #3f75ff;
  color: #fff;
  border-radius: 4px 4px 4px 4px;
}

/deep/ .el-table__cell {
  text-align: center;
}
</style>
